﻿package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.components.CRender;
	import CutLoad.components.CTextField;
	import CutLoad.components.Skin;
	import CutLoad.cookies.Painter;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.text.TextField;
	/**
	 * 提示框 (鼠标放上去显提示信息的提示框)
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */
	public class CTip extends Base
	{
		protected var bak:Sprite=null;
		private var bakBorderLeftRight:Number=0
		private var bakBorderLeftUpDown:Number=0
		protected var startX:Number,startY:Number;
		
		private var txt:CTextField=null;
		private var img:Bitmap=null;
		private var tipw:Number;
		private var currentStr:String="";
		private var bakRender:CRender=null;
		//
		/**
		 * 
		 * @param str 显示文本内容:可以带有html代码
		 * @param w 宽度，默认为0,则不自动换行
		 * 
		 */		
		public function CTip(str:String,w:Number=0)
		{
			var sysKinTem:Array=Skin.getInstance().getSkin(4);
			setUiBak()
			startX=sysKinTem[1];
			startY=sysKinTem[2];
			bakBorderLeftRight=sysKinTem[3]
			bakBorderLeftUpDown=sysKinTem[4]
			setBakColor();
			//
			setText(str,w);
			currentStr=str;
		}
		/**
		 * 设置文本内容 
		 * @param str 文本内容
		 * @param w 宽度
		 * 
		 */		
		public function setText(str:String,w:Number=0):void{
			//如果设置的值，就是现在这个值，就不再设置了。
			if(currentStr==str) return;
			setUiBak()
			if(img!=null) this.removeChild(img);
			if(txt!=null) this.removeChild(txt);
			
			tipw=w;
			//
			txt=new CTextField();
			txt.init();
			txt.x=this.startX;
			txt.y=this.startY;
			var txtfield:TextField=txt.getTextField();
			if(tipw!=0){
				txtfield.width=tipw;
				txtfield.wordWrap=true;
			}
			txt.setAutoSize(1);
			txt.show(this);
			txt.htmlText=str;
			currentStr=str;
			//
			var ww:Number=txtfield.textWidth+this.startX*2
			var hh:Number=txtfield.textHeight+this.startY*2;
			//
			setbakWH(ww,hh);
			if(bakRender!=null) bakRender.setWH(ww,hh);
			//把tip生成一个图片
			img=Painter.getImg(this,ww,hh);
			this.addChild(img);
			this.removeChild(bak);
			this.removeChild(txt);
			txt=null;
			bak=null
			clrBak()
		}
		/**
		 * 添加背景框
		 * 
		 */		
		protected function setUiBak():void{
			if(bak!=null) this.removeChild(bak);
			var sysKinTem:Array=Skin.getInstance().getSkin(4);
			bak=Base.getStrClassObject(sysKinTem[0]) as Sprite;
			this.addChild(bak);
		}
		/**
		 * 设置背景宽度 
		 * @param w
		 * @param h
		 * 
		 */		
		protected function setbakWH(w:Number=0, h:Number=0):void{
			if(bak==null) return;
			bak.width=w;
			bak.height=h;
		}
		/**
		 * 设置背景
		 *
		 */	
		public function setBak(way:uint,type:uint,config:Object):void{
			clrBak()
			bakRender=new CRender();
			bakRender.init(way,type,config,this.getShowWidth()-bakBorderLeftRight*2,this.getShowHeight()-this.bakBorderLeftUpDown*2);
			bakRender.setXY(this.bakBorderLeftRight,this.bakBorderLeftUpDown);
			bakRender.show(this);
			this.setText(currentStr,this.tipw);
		}
		//
		/**
		 * 设置一个色块的背景
		 * @param c 颜色
		 * @param a 透明度
		 * 
		 */		
		public function setBakColor(c:Number=0xffffff,a:Number=0.5,isBitmap:Boolean=false):void{
			setBak(0,0,{color:c,alpha:a,isBitmap:isBitmap})
		}
		/**
		 * 清除背景 
		 * @return 
		 * 
		 */		
		private function clrBak():void{
			if(bakRender!=null){
				this.removeChild(bakRender);
				bakRender=null;
			}
		}
		override public function setWH(w:Number=0, h:Number=0):void{
			throw new Error("此方法已禁用，因为这里不需要设置宽高!");
		}
		
	}
}